package com.zhicheng.controller;


import com.zhicheng.pojo.AddressBook;
import com.zhicheng.pojo.result.PageResult;
import com.zhicheng.pojo.result.Result;
import com.zhicheng.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Api(tags = "地址簿相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/address/list")
    @ApiOperation("查询当前用户的所有地址信息")
    public Result<List<AddressBook>> list(){
        List<AddressBook> addressBooks = addressBookService.list();
        return Result.success(addressBooks);
    }

    @PostMapping("/address/add")
    @ApiOperation("添加用户收货地址信息")
    private Result add(@RequestBody AddressBook addressBook) {
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    @PutMapping("/address/update")
    @ApiOperation("修改用户收货地址信息")
    private Result update(@RequestBody AddressBook addressBook) {
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    @DeleteMapping("/address/delete")
    @ApiOperation("删除用户收货地址信息")
    private Result delete(Long id) {
        addressBookService.deleteAddress(id);
        return Result.success();
    }

    @GetMapping("/member/address")
    @ApiOperation("根据用户id分页查询地址")
    public Result<PageResult<AddressBook>> getByUserId(int page, int pageSize){
        PageResult<AddressBook> pageResult = addressBookService.getByUserId(page,pageSize);
        return Result.success(pageResult);
    }



}
